package it.salvatore.parser;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class SimpleSearchTask implements Runnable{

	private String jsessionid;
	private String searchString;
	private RemoteServiceClientListener mCallBack;
	
	public SimpleSearchTask(String searchString,
			RemoteServiceClientListener mCallBack, String jsessionid) {
		super();
		this.jsessionid = jsessionid;
		this.searchString = searchString;
	}



	@Override
	public void run() {
		//TODO Spostare URL nelle risorse
		//ex URL Ricerca: http://opac.provincia.bergamo.it/public/opac/documentSearchResult.seam;jsessionid=4D09FAF1928F20A1881B820E8F9DDC32?type=type.pdf&cid=58899
		Response res;
		boolean successful=false;
		ArrayList result = null;
		int reason;
		try {
			//TODO verificare come generare il cid

			res = Jsoup.connect("http://opac.provincia.bergamo.it/public/opac/documentSearchResult.seam;jsessionid="+jsessionid+"?type=type.pdf&cid=58899")					
					.data("opacFulltext:fullText",searchString,"javax.faces.ViewState","j_id7").method(Method.POST)
					.execute();

			Document doc = res.parse();	 

		
			
		} catch (IOException e) {
			mCallBack.searchFailed(e);
			
		}
		 
		if (successful){
			mCallBack.searchPerformed(result);
		}
		else
		{
			mCallBack.searchFailed(404);
		}



	}

}
